package com.tjn.arrays_;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 陶镓妮
 * @version 1.0
 */
public class ArraysExercise {
    public static void main(String[] args) {
        Book[] book = new Book[4];
        book[0] = new Book("红楼梦",100);
        book[1] = new Book("金瓶梅新",90);
        book[2] = new Book("青年文摘20年",50);
        book[3] = new Book("java入门",200);

        //价格从小到大排序
        Arrays.sort(book, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book b1 = (Book)o1;
                Book b2 = (Book)o2;
                return (int)(b1.getPrice()-b2.getPrice());
            }
        });
        System.out.println(Arrays.toString(book));

        //价格从大到小排序
        Arrays.sort(book, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book b1 = (Book)o1;
                Book b2 = (Book)o2;
                return (int)(b2.getPrice()-b1.getPrice());
            }
        });
        System.out.println(Arrays.toString(book));

        //书名长度从小到大排序
        Arrays.sort(book, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;
                return b1.getName().length()-b2.getName().length();
            }
        });
        System.out.println(Arrays.toString(book));
    }
}

class Book{
    private String name;
    private double price;

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}